
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>单例模式 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../../../../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../../../../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../../../../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../../../../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../../structuredmodel/" />
    
    
    <link rel="prev" href="../prototypemode/" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../../../../../">
            
                <a href="../../../../../../">
            
                    
                    说明
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../../../../../">
            
                <a href="../../../../../">
            
                    
                    设计模式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" >
            
                <span>
            
                    
                    创建型模式
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1.1" >
            
                <span>
            
                    
                    简单工厂模式
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.1.2" >
            
                <span>
            
                    
                    工厂方法模式
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.1.3" data-path="../abstractfactorymode/">
            
                <a href="../abstractfactorymode/">
            
                    
                    抽象工厂模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.1.4" data-path="../bulidermode/">
            
                <a href="../bulidermode/">
            
                    
                    建造者模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.1.5" data-path="../prototypemode/">
            
                <a href="../prototypemode/">
            
                    
                    原型模式
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.1.1.6" data-path="./">
            
                <a href="./">
            
                    
                    单例模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="../../structuredmodel/">
            
                <a href="../../structuredmodel/">
            
                    
                    结构型模式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.2.1" data-path="../../structuredmodel/proxymode/">
            
                <a href="../../structuredmodel/proxymode/">
            
                    
                    代理模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2.2" data-path="../../structuredmodel/adaptermode/">
            
                <a href="../../structuredmodel/adaptermode/">
            
                    
                    适配器模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2.3" data-path="../../structuredmodel/bridgemode/">
            
                <a href="../../structuredmodel/bridgemode/">
            
                    
                    桥接模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2.4" data-path="../../structuredmodel/decoratormode/">
            
                <a href="../../structuredmodel/decoratormode/">
            
                    
                    装饰模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2.5" data-path="../../structuredmodel/facademode/">
            
                <a href="../../structuredmodel/facademode/">
            
                    
                    外观模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2.6" data-path="../../structuredmodel/flyweightmode/">
            
                <a href="../../structuredmodel/flyweightmode/">
            
                    
                    享元模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2.7" data-path="../../structuredmodel/compositemode/">
            
                <a href="../../structuredmodel/compositemode/">
            
                    
                    组合模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="../../behavioralmodel/">
            
                <a href="../../behavioralmodel/">
            
                    
                    行为型模式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.3.1" data-path="../../behavioralmodel/templatemethodmode/">
            
                <a href="../../behavioralmodel/templatemethodmode/">
            
                    
                    模板方法模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.2" data-path="../../behavioralmodel/strategymode/">
            
                <a href="../../behavioralmodel/strategymode/">
            
                    
                    策略模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.3" data-path="../../behavioralmodel/commandmode/">
            
                <a href="../../behavioralmodel/commandmode/">
            
                    
                    命令模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.4" data-path="../../behavioralmodel/chainofresponsibility/">
            
                <a href="../../behavioralmodel/chainofresponsibility/">
            
                    
                    责任链模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.5" data-path="../../behavioralmodel/statemode/">
            
                <a href="../../behavioralmodel/statemode/">
            
                    
                    状态模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.6" data-path="../../behavioralmodel/observermode/">
            
                <a href="../../behavioralmodel/observermode/">
            
                    
                    观察者模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.7" data-path="../../behavioralmodel/mediatormode/">
            
                <a href="../../behavioralmodel/mediatormode/">
            
                    
                    中介者模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.8" data-path="../../behavioralmodel/iteratormode/">
            
                <a href="../../behavioralmodel/iteratormode/">
            
                    
                    迭代器模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.9" data-path="../../behavioralmodel/visitormode/">
            
                <a href="../../behavioralmodel/visitormode/">
            
                    
                    访问者模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.10" data-path="../../behavioralmodel/mementomode/">
            
                <a href="../../behavioralmodel/mementomode/">
            
                    
                    备忘录模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3.11" data-path="../../behavioralmodel/interpretermode/">
            
                <a href="../../behavioralmodel/interpretermode/">
            
                    
                    解释器模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../../../../../../Nacos/">
            
                <a href="../../../../../../Nacos/">
            
                    
                    Nacos
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="../../../../../../Nacos/Nacos-Introduction.html">
            
                <a href="../../../../../../Nacos/Nacos-Introduction.html">
            
                    
                    Nacos介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="../../../../../../Nacos/Nacos-Registration.html">
            
                <a href="../../../../../../Nacos/Nacos-Registration.html">
            
                    
                    Nacos注册中心
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.3" data-path="../../../../../../Nacos/Nacos-OpenFeign.html">
            
                <a href="../../../../../../Nacos/Nacos-OpenFeign.html">
            
                    
                    Nacos+OpenFeign
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../../../../../../Sortings/">
            
                <a href="../../../../../../Sortings/">
            
                    
                    排序算法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1.1" data-path="../../../../../../Sortings/src/main/java/com/larscheng/www/InsertionSort/">
            
                <a href="../../../../../../Sortings/src/main/java/com/larscheng/www/InsertionSort/">
            
                    
                    插入排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.2" data-path="../../../../../../Sortings/src/main/java/com/larscheng/www/shellSort/">
            
                <a href="../../../../../../Sortings/src/main/java/com/larscheng/www/shellSort/">
            
                    
                    希尔排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.3" >
            
                <span>
            
                    
                    选择排序
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.4" >
            
                <span>
            
                    
                    堆排序
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.5" >
            
                <span>
            
                    
                    希尔排序
            
                </span>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../../../../.." >单例模式</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h3 id="&#x5355;&#x4F8B;&#x6A21;&#x5F0F;">&#x5355;&#x4F8B;&#x6A21;&#x5F0F;</h3>
<p>&#x4E3A;&#x4E86;&#x8282;&#x7701;&#x7CFB;&#x7EDF;&#x8D44;&#x6E90;&#x3001;&#x4FDD;&#x8BC1;&#x6570;&#x636E;&#x7684;&#x4E00;&#x81F4;&#x6027;&#x3001;&#x6216;&#x8005;&#x4E00;&#x4E9B;&#x5176;&#x4ED6;&#x7684;&#x9700;&#x6C42;&#x3001;&#x67D0;&#x4E9B;&#x7C7B;&#x5728;&#x7CFB;&#x7EDF;&#x4E2D;&#x53EA;&#x80FD;&#x5B58;&#x5728;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#xFF0C;&#x5B9E;&#x73B0;&#x8FD9;&#x4E00;&#x573A;&#x666F;&#x7684;&#x65B9;&#x5F0F;&#x5C31;&#x79F0;&#x4F5C;&#x5355;&#x4F8B;&#x6A21;&#x5F0F;</p>
<p>eg&#xFF1A;windows&#x4EFB;&#x52A1;&#x7BA1;&#x7406;&#x5668;&#x3001;&#x591A;&#x7EBF;&#x7A0B;&#x4E2D;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;&#x3001;&#x56DE;&#x6536;&#x7AD9;&#x3001;&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;&#x6C60;....</p>
<h3 id="&#x7279;&#x70B9;">&#x7279;&#x70B9;</h3>
<p>&#x5355;&#x4F8B;&#x7C7B;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;</p>
<p>&#x5355;&#x4F8B;&#x5BF9;&#x8C61;&#x5FC5;&#x987B;&#x7531;&#x5355;&#x4F8B;&#x7C7B;&#x81EA;&#x884C;&#x521B;&#x5EFA;</p>
<p>&#x5355;&#x4F8B;&#x7C7B;&#x5BF9;&#x5916;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x8BBF;&#x95EE;&#x8BE5;&#x5B9E;&#x4F8B;&#x7684;&#x5168;&#x5C40;&#x8BBF;&#x95EE;&#x70B9;</p>
<h3 id="&#x8981;&#x7D20;">&#x8981;&#x7D20;</h3>
<p>&#x5355;&#x4F8B;&#x7C7B;&#x3001;&#x8BBF;&#x95EE;&#x7C7B;</p>
<h3 id="&#x5B9E;&#x73B0;">&#x5B9E;&#x73B0;</h3>
<ul>
<li><p>&#x61D2;&#x6C49;&#x5F0F; : &#x7C7B;&#x52A0;&#x8F7D;&#x65F6;&#x6CA1;&#x6709;&#x751F;&#x6210;&#x5355;&#x4F8B;&#xFF0C;&#x53EA;&#x6709;&#x5F53;&#x7B2C;&#x4E00;&#x6B21;&#x8C03;&#x7528;&#x8BBF;&#x95EE;&#x70B9;&#x65F6;&#x91C7;&#x53D6;&#x521B;&#x5EFA;&#x5B9E;&#x4F8B;</p>
</li>
<li><p>&#x997F;&#x6C49;&#x5F0F; : &#x7C7B;&#x4E00;&#x65E6;&#x52A0;&#x8F7D;&#x5C31;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5355;&#x4F8B;&#xFF0C;&#x4FDD;&#x8BC1;&#x5728;&#x8C03;&#x7528; getInstance &#x65B9;&#x6CD5;&#x4E4B;&#x524D;&#x5355;&#x4F8B;&#x5DF2;&#x7ECF;&#x5B58;&#x5728;&#x4E86;</p>
</li>
</ul>
<p><img src="http://c.biancheng.net/uploads/allimg/181113/3-1Q1131K441K2.gif" alt=""></p>
<h3 id="&#x7EBF;&#x7A0B;&#x5B89;&#x5168;">&#x7EBF;&#x7A0B;&#x5B89;&#x5168;</h3>
<p>&#x61D2;&#x6C49;&#x5F0F;&#xFF1A;&#x7EBF;&#x7A0B;&#x975E;&#x5B89;&#x5168;&#xFF1B;</p>
<p>&#x997F;&#x6C49;&#x5F0F;&#xFF1A;&#x7EBF;&#x7A0B;&#x5B89;&#x5168;</p>
<h3 id="&#x61D2;&#x6C49;&#x5F0F;-&#x7EBF;&#x7A0B;&#x5B89;&#x5168;&#x7684;&#x63AA;&#x65BD;&#xFF1A;">&#x61D2;&#x6C49;&#x5F0F;-&#x7EBF;&#x7A0B;&#x5B89;&#x5168;&#x7684;&#x63AA;&#x65BD;&#xFF1A;</h3>
<h4 id="&#x53CC;&#x91CD;&#x5224;&#x65AD;&#x52A0;&#x9501;">&#x53CC;&#x91CD;&#x5224;&#x65AD;&#x52A0;&#x9501;</h4>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Singleton</span></span>{
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">volatile</span> Singleton singleton = <span class="hljs-keyword">null</span>;
    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-title">Singleton</span><span class="hljs-params">()</span></span>{}
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Singleton <span class="hljs-title">getInstance</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">if</span> (singleton==<span class="hljs-keyword">null</span>){
            <span class="hljs-keyword">synchronized</span> (Singleton.class){
                <span class="hljs-keyword">if</span> (singleton==<span class="hljs-keyword">null</span>){
                    singleton=<span class="hljs-keyword">new</span> Singleton();
                }
            }
            <span class="hljs-keyword">return</span> singleton;
        }
    }
}
</code></pre>
<h4 id="&#x9759;&#x6001;&#x5185;&#x90E8;&#x7C7B;">&#x9759;&#x6001;&#x5185;&#x90E8;&#x7C7B;</h4>
<p>Singleton &#x7C7B;&#x88AB;&#x88C5;&#x8F7D;&#x4E86;&#xFF0C;singleton &#x4E0D;&#x4E00;&#x5B9A;&#x88AB;&#x521D;&#x59CB;&#x5316;&#x3002;&#x56E0;&#x4E3A; SingletonHolder &#x7C7B;&#x6CA1;&#x6709;&#x88AB;&#x4E3B;&#x52A8;&#x4F7F;&#x7528;&#xFF0C;
&#x53EA;&#x6709;&#x901A;&#x8FC7;&#x663E;&#x5F0F;&#x8C03;&#x7528; getInstance &#x65B9;&#x6CD5;&#x65F6;&#xFF0C;&#x624D;&#x4F1A;&#x663E;&#x5F0F;&#x88C5;&#x8F7D; SingletonHolder &#x7C7B;&#xFF0C;&#x4ECE;&#x800C;&#x5B9E;&#x4F8B;&#x5316; singleton&#x3002;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Singleton</span></span>{
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SingletonHolder</span></span>{
        <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> Singleton singleton = <span class="hljs-keyword">new</span> Singleton();
    }
    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-title">Singleton</span><span class="hljs-params">()</span></span>{}
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Singleton <span class="hljs-title">getInstance</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">return</span> SingletonHolder.singleton;
    }
}
</code></pre>
<h3 id="&#x53CD;&#x5C04;&#x673A;&#x5236;&#x7834;&#x89E3;&#x5355;&#x4F8B;&#x6A21;&#x5F0F;">&#x53CD;&#x5C04;&#x673A;&#x5236;&#x7834;&#x89E3;&#x5355;&#x4F8B;&#x6A21;&#x5F0F;</h3>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Singleton</span> </span>{
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> Singleton singleton= <span class="hljs-keyword">new</span> Singleton();

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-title">singleton</span><span class="hljs-params">()</span> </span>{
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Singleton <span class="hljs-title">newInstance</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> singleton;
    }
}
</code></pre>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BreakSingleton</span></span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span></span>{
        Class aClass = Class.forName(<span class="hljs-string">&quot;Singleton&quot;</span>);
        Constructor c = aClass.getDeclaredConstructor(<span class="hljs-keyword">null</span>);
        c.setAccessible(<span class="hljs-keyword">true</span>);
        Singleton singleton1 = c.newInstance();
        Singleton singleton2 = c.newInstance(); 
    }
}
</code></pre>
<p>&#x9884;&#x9632;&#x53CD;&#x5C04;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Singleton</span> </span>{
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> Singleton singleton= <span class="hljs-keyword">new</span> Singleton();

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-title">singleton</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-comment">//&#x9632;&#x6B62;&#x901A;&#x8FC7;&#x53CD;&#x5C04;&#x521B;&#x5EFA;&#x5BF9;&#x8C61;</span>
        <span class="hljs-keyword">if</span> (singleton!=<span class="hljs-keyword">null</span>){
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> RuntimeException();
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Singleton <span class="hljs-title">newInstance</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> singleton;
    }
}
</code></pre>
<h3 id="&#x53CD;&#x5E8F;&#x5217;&#x5316;&#x673A;&#x5236;&#x7834;&#x89E3;&#x5355;&#x4F8B;&#x6A21;&#x5F0F;">&#x53CD;&#x5E8F;&#x5217;&#x5316;&#x673A;&#x5236;&#x7834;&#x89E3;&#x5355;&#x4F8B;&#x6A21;&#x5F0F;</h3>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BreakSingleton</span></span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span></span>{
      Singleton singleton1 = Singleton.getInstance();
      Singleton singleton2 = Singleton.getInstance();

      FileOutputStream fos = <span class="hljs-keyword">new</span> FileOutputStream(<span class="hljs-string">&quot;&#x8DEF;&#x5F84;&quot;</span>);
      ObjectOutputStream oos = <span class="hljs-keyword">new</span> ObjectOutputStream(fos);
      oss.writeObject(singleton1);
      oss.close();
      fos.close();

      ObjectInputStream ois = <span class="hljs-keyword">new</span> ObjectInputStream(<span class="hljs-keyword">new</span> FileInputStream(<span class="hljs-string">&quot;&#x8DEF;&#x5F84;&quot;</span>));
      Singleton singleton3 = (Singleton)ois.readObject();
      System.out.println(<span class="hljs-string">&quot;singleton1==singleton3:&quot;</span>+singleton1==singleton3);
    }
}
</code></pre>
<p>&#x9884;&#x9632;&#x63AA;&#x65BD;&#xFF1A;</p>
<pre><code class="lang-java">
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Singleton</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Serializable</span></span>{
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> Singleton singleton= <span class="hljs-keyword">new</span> Singleton();

        <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-title">singleton</span><span class="hljs-params">()</span> </span>{
        }

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Singleton <span class="hljs-title">newInstance</span><span class="hljs-params">()</span> </span>{
            <span class="hljs-keyword">return</span> singleton;
        }
        <span class="hljs-comment">//&#x53CD;&#x5E8F;&#x5217;&#x5316;&#x5B9A;&#x4E49;&#x8BE5;&#x65B9;&#x6CD5;&#xFF0C;&#x5219;&#x4E0D;&#x9700;&#x8981;&#x521B;&#x5EFA;&#x65B0;&#x5BF9;&#x8C61;</span>
        <span class="hljs-function"><span class="hljs-keyword">private</span> Object <span class="hljs-title">readResovle</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> ObjectStreamException</span>{
            <span class="hljs-keyword">return</span> singleton;
        }
}
</code></pre>
<h3 id="&#x679A;&#x4E3E;&#x5B9E;&#x73B0;&#x5355;&#x4F8B;">&#x679A;&#x4E3E;&#x5B9E;&#x73B0;&#x5355;&#x4F8B;</h3>
<p><a href="https://blog.csdn.net/yy254117440/article/details/52305175" target="_blank">https://blog.csdn.net/yy254117440/article/details/52305175</a></p>
<p>&#x679A;&#x4E3E;&#x672C;&#x8EAB;&#x5C31;&#x5B9E;&#x73B0;&#x4E86;&#x5355;&#x4F8B;&#xFF0C;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x79C1;&#x6709;&#xFF0C;&#x5E76;&#x4E14;&#x7EBF;&#x7A0B;&#x5B89;&#x5168;&#xFF0C;&#x4F46;&#x662F;&#x4E0D;&#x80FD;&#x5B9E;&#x73B0;&#x5EF6;&#x8FDF;&#x52A0;&#x8F7D;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">enum</span>  SingletonEnum {
    <span class="hljs-comment">/**
     * &#x5355;&#x4F8B;&#x7684;&#x63A8;&#x8350;&#x7528;&#x6CD5;
     */</span>
    INSTANCE;

    <span class="hljs-keyword">private</span> Person person;
    SingletonEnum() {
        System.out.println(<span class="hljs-string">&quot;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x88AB;&#x8C03;&#x7528;&quot;</span>);
        person = <span class="hljs-keyword">new</span> Person();
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> Person <span class="hljs-title">getInstance</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">return</span> person;
    }

    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span></span>{}
}
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../prototypemode/" class="navigation navigation-prev " aria-label="Previous page: 原型模式">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../../structuredmodel/" class="navigation navigation-next " aria-label="Next page: 结构型模式">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"单例模式","level":"2.1.1.6","depth":3,"next":{"title":"结构型模式","level":"2.1.2","depth":2,"path":"Design-Patterns/src/main/java/structuredmodel/README.md","ref":"Design-Patterns/src/main/java/structuredmodel/README.md","articles":[{"title":"代理模式","level":"2.1.2.1","depth":3,"path":"Design-Patterns/src/main/java/structuredmodel/proxymode/README.md","ref":"Design-Patterns/src/main/java/structuredmodel/proxymode/README.md","articles":[]},{"title":"适配器模式","level":"2.1.2.2","depth":3,"path":"Design-Patterns/src/main/java/structuredmodel/adaptermode/README.md","ref":"Design-Patterns/src/main/java/structuredmodel/adaptermode/README.md","articles":[]},{"title":"桥接模式","level":"2.1.2.3","depth":3,"path":"Design-Patterns/src/main/java/structuredmodel/bridgemode/README.md","ref":"Design-Patterns/src/main/java/structuredmodel/bridgemode/README.md","articles":[]},{"title":"装饰模式","level":"2.1.2.4","depth":3,"path":"Design-Patterns/src/main/java/structuredmodel/decoratormode/README.md","ref":"Design-Patterns/src/main/java/structuredmodel/decoratormode/README.md","articles":[]},{"title":"外观模式","level":"2.1.2.5","depth":3,"path":"Design-Patterns/src/main/java/structuredmodel/facademode/README.md","ref":"Design-Patterns/src/main/java/structuredmodel/facademode/README.md","articles":[]},{"title":"享元模式","level":"2.1.2.6","depth":3,"path":"Design-Patterns/src/main/java/structuredmodel/flyweightmode/README.md","ref":"Design-Patterns/src/main/java/structuredmodel/flyweightmode/README.md","articles":[]},{"title":"组合模式","level":"2.1.2.7","depth":3,"path":"Design-Patterns/src/main/java/structuredmodel/compositemode/README.md","ref":"Design-Patterns/src/main/java/structuredmodel/compositemode/README.md","articles":[]}]},"previous":{"title":"原型模式","level":"2.1.1.5","depth":3,"path":"Design-Patterns/src/main/java/createdmodel/prototypemode/README.md","ref":"Design-Patterns/src/main/java/createdmodel/prototypemode/README.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["expandable-chapters","livereload"],"pluginsConfig":{"expandable-chapters":{},"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"Design-Patterns/src/main/java/createdmodel/singletonmode/README.md","mtime":"2019-07-08T11:05:14.977Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-09-09T07:20:35.937Z"},"basePath":"../../../../../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../../../../../gitbook/gitbook.js"></script>
    <script src="../../../../../../gitbook/theme.js"></script>
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../../../../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

